
from pysimplesoap.server import SoapDispatcher, SOAPHandler
from BaseHTTPServer import HTTPServer
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
import smtplib


def MandaMail(destinatario, messaggio, soggetto, img_locandina, img_biglietto):
    mittente = "centrosportivosscsweb@gmail.com"
    password = "sscsweb1314"
    msg = MIMEMultipart()
    msg['From'] = mittente
    msg['To'] = destinatario
    msg['Subject'] = soggetto
    body = messaggio
    msg.attach(MIMEText(body, 'html'))

    if img_locandina != " " or img_biglietto != " ":
        fp = open(img_biglietto, 'rb')
        msgImage = MIMEImage(fp.read())
        fp.close()

        msgImage.add_header('Content-ID', '<image_biglietto>')
        msg.attach(msgImage)

        fp = open(img_locandina, 'rb')
        msgImage = MIMEImage(fp.read())
        fp.close()

        msgImage.add_header('Content-ID', '<image_locandina>')
        msg.attach(msgImage)

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login(mittente,password)
    text = msg.as_string()
    server.sendmail(mittente, destinatario, text)
    return True

dispatcher = SoapDispatcher(
    'my_dispatcher',
    location="http://localhost:8008/",
    action='http://localhost:8008/',  # SOAPAction
    namespace="http://example.com/sample.wsdl", prefix="ns0",
    trace=True,
    ns=True)


dispatcher.register_function('MandaMail', MandaMail, returns={'MandaMailResult': bool}, args={'destinatario': str, 'messaggio': str, 'soggetto': str, 'img_locandina': str, 'img_biglietto': str})

print "Starting server..."
httpd = HTTPServer(("", 8008), SOAPHandler)
httpd.dispatcher = dispatcher
httpd.serve_forever()
